package com.sikao.app.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.bean.CategoryAttrNewBean;
import com.sikao.app.ui.AddGoodsStepTwoActivity;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布商品第二步 类型是5 下拉的子项 有浮动值
 */
public class MllAddStepPricesTypeFiveChildItemNewAdapter extends BaseAdapter {
    CategoryAttrNewBean.ResBean.AttrBean resBean;//父级
    private List<CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean> objects = new ArrayList<CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean>();
    List<CategoryAttrNewBean.ResBean.AttrBean.DefaultValuesBean> defaultValueBeanList = new ArrayList<>();//默认值
    private Context context;
    private LayoutInflater layoutInflater;
    int index = 0;
     int count=0;
    public MllAddStepPricesTypeFiveChildItemNewAdapter(Context context, CategoryAttrNewBean.ResBean.AttrBean resBean, int index) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.resBean = resBean;
        objects = resBean.getAttr_values();
        defaultValueBeanList = resBean.getDefault_value();
        this.index = index;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

       //   if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.mll_add_step_prices_type_five_child_new_item, null);
        View defau = convertView;
        if(!((MyListView)parent).isOnMeasure) {
            return defau;
        }

        convertView.setTag(new ViewHolder(convertView));
        convertView.setId(index);
       //  }
        doSelected(position, convertView, parent);
        initializeViews(getItem(position), (ViewHolder) convertView.getTag(), position,convertView,parent);

            return convertView;

    }


    private void doSelected(int position, View convertView, ViewGroup parent) {
        //默认值
        //Utils.showLog("====avb进入默认值"+AddGoodsStepTwoActivity.initFlagTypeFive);
        if (!AddGoodsStepTwoActivity.initFlagTypeFive) {

            CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean object = getItem(position);
            ViewHolder holder = (ViewHolder) convertView.getTag();
            for (int i = 0; i < AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().size(); i++) {
                if (object.getId() == AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().get(i).getId()) {
                    holder.cbFiveChildIsUse.setChecked(true);

                    holder.cbFiveChildIsDefault.setEnabled(true);
                    holder.cbFiveChildIsDefault.setClickable(true);
                    //判断是不是默认值
                     if ("1".equals(AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().get(i).isDefault())){
                         holder.cbFiveChildIsDefault.setChecked(true);
                     }
                    holder.etFiveChildChangePrice.setEnabled(true);
                    holder.etFiveChildChangePrice.setClickable(true);

                    //修改单选数据组 属性值
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChecked(true);

                    //给默认的浮动值复制
                    holder.etFiveChildChangePrice.setText(AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().get(i).getChangePrice());
                    //将默认值中的值附加到要提交的数据上
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setDefault(AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().get(i).isDefault());
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChangePrice(AddGoodsStepTwoActivity.resTypeFive.get(index).getDefault_value().get(i).getChangePrice());


                }

                if (parent != null) {
                    int size_ = parent.getChildCount();
                    //检查当前列表的个数，与数据的个数是否相等，即ListView已经初步加载完成
                    if (size_ > 0 && objects.size() == size_) {//至此，可以判定该处理接口已经成功执行过一次
                        Utils.showLog("====avb完成默认值 子总数量" + size_);
                        AddGoodsStepTwoActivity.initFlagTypeFive = true;
                    }
                }
            }
        }
    }

    private void initializeViews(CategoryAttrNewBean.ResBean.AttrBean.AttrValuesBean object, final ViewHolder holder, final int position, final View convertView, final ViewGroup parent) {
        //TODO implement
        holder.tvFiveChildValue.setText(object.getValue());
        if (AddGoodsStepTwoActivity.canEdit){
            holder.cbFiveChildIsUse.setEnabled(true);
            holder.cbFiveChildIsUse.setClickable(true);
          /*  holder.etFiveChildChangePrice.setEnabled(true);
            holder.etFiveChildChangePrice.setClickable(true);*/
        }else{
            holder.cbFiveChildIsUse.setEnabled(false);
            holder.cbFiveChildIsUse.setClickable(false);
            holder.etFiveChildChangePrice.setEnabled(false);
            holder.etFiveChildChangePrice.setClickable(false);
        }
        holder.cbFiveChildIsUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    holder.etFiveChildChangePrice.setEnabled(true);
                    holder.etFiveChildChangePrice.setClickable(true);
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChecked(true);
                    //默认是可以选择状态
                    holder.cbFiveChildIsDefault.setEnabled(true);
                    holder.cbFiveChildIsDefault.setClickable(true);
                } else {
                    holder.etFiveChildChangePrice.setEnabled(false);
                    holder.etFiveChildChangePrice.setClickable(false);
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChecked(false);
                    //默认是不可以选择状态
                    holder.cbFiveChildIsDefault.setEnabled(false);
                    holder.cbFiveChildIsDefault.setClickable(false);
                    //同时取消选中默认状态
                    holder.cbFiveChildIsDefault.setChecked(false);
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setDefault(false);
                }
            }
        });

        holder.cbFiveChildIsDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((CheckBox) v).isChecked()) {
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setDefault(true);
                    //保证默认值只有一个

                    for (int i=0;i<AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().size();i++){
                       if (AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(i).isDefault()&&i!=position){
                           //将是否默认值设置为0 取消选中状态
                           AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(i).setDefault(false);
                           ( (ViewHolder)   parent.getChildAt(i).getTag()).cbFiveChildIsDefault.setChecked(false);
                       }
                    }

                } else {
                    AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setDefault(false);
                }
            }
        });

       /* holder.etFiveChildChangePrice.setOnTouchListener(new View.OnTouchListener() {

            EditText et = null;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                et = (EditText) v;

                et.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // String  changePrice = et.getText().toString();
                        AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChangePrice(et.getText().toString());
                    }
                });
                return false;
            }
        });
*/
        holder.etFiveChildChangePrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                AddGoodsStepTwoActivity.resTypeFive.get(index).getAttr_values().get(position).setChangePrice(editable.toString());
            }
        });
    }


    protected class ViewHolder {
        private CheckBox cbFiveChildIsUse;
        private CheckBox cbFiveChildIsDefault;
        private TextView tvFiveChildValue;
        private EditText etFiveChildChangePrice;

        public ViewHolder(View view) {
            cbFiveChildIsDefault = (CheckBox) view.findViewById(R.id.cb_five_child_isdefault);
            cbFiveChildIsUse = (CheckBox) view.findViewById(R.id.cb_five_child_isuse);

            tvFiveChildValue = (TextView) view.findViewById(R.id.tv_five_child_value);
            etFiveChildChangePrice = (EditText) view.findViewById(R.id.et_five_child_change_price);
        }
    }
}
